package fr.greta.centre.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;

import fr.greta.centre.metier.User;

public class UserDAOImpl implements UserDAO{

	@Override
	public User findByLoginAndPassword(String login, String password)
			throws SQLException {
		
		assert login != null && login.matches("^(\\s)*$");
		assert password != null && password.matches("^(\\s)*$");
		
		User unUser = null;
		PreparedStatement st;
		
		try {
			Connection conn = Bd.getConnect();
			String sql = "SELECT * FROM user WHERE login=? AND pwd=?";
			st = conn.prepareStatement(sql);
			st.setString(1, login);
			st.setString(2, password);
			ResultSet resultat = st.executeQuery();

			if (resultat.next()) {
				unUser = new User(resultat.getInt("id_user"),
						resultat.getString("login"),
						resultat.getString("pwd"));
			}
			conn.close();
			return unUser;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
			
		}	
	}

	@Override
	public int save(User t) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User findByKey(int id) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
